package fragmentparent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import activity.SonActivity;
import bean.MessageForSimple;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.Constant;
import util.cache.ImageLoader;
import util.myview.CircleImageView;

/**
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class Fragment_D extends Fragment {

	private View rootView;
	private Intent intent;
	private Bundle bundle;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.activity_fragment_d, container, false);
		initView();
		return rootView;
	}

	@Override
	public void onResume() {
		super.onResume();
		initData();
	}

	private void initView(){
		rootView.findViewById(R.id.ll_password).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_photo).setOnClickListener(listener);
		rootView.findViewById(R.id.ll_money).setOnClickListener(listener);
		((TextView) rootView.findViewById(R.id.tv_version)).setText("v"+ DemoApplication.getVersion());
	}


	public void initData(){
		ApiUtil.getApiService().getUserinfo(DemoApplication.getToken()).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						((TextView) rootView.findViewById(R.id.tv_nice)).setText(entity.getData().getUsername());
						((TextView) rootView.findViewById(R.id.tv_phone)).setText(entity.getData().getTelephone());
						new ImageLoader(getActivity()).DisplayImage(entity.getData().getHeadimg(),((CircleImageView) rootView.findViewById(R.id.iv_photo)));
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}


	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_photo:
					intent = new Intent(getActivity(), SonActivity.class);
					bundle = new Bundle();
					bundle.putInt("code", Constant.UserInfo);
					intent.putExtras(bundle);
					startActivity(intent);
					break;
				case R.id.ll_password:
					intent = new Intent(getActivity(), SonActivity.class);
					bundle = new Bundle();
					bundle.putInt("code", Constant.ChangePassword);
					intent.putExtras(bundle);
					startActivity(intent);
					break;
				case R.id.ll_money:
					intent = new Intent(getActivity(), SonActivity.class);
					bundle = new Bundle();
					bundle.putInt("code", Constant.RemainingSum);
					intent.putExtras(bundle);
					startActivity(intent);
					break;
				default:
					break;
			}
		}
	};

	public static Fragment_D newInstance() {
		Fragment_D fragment = new Fragment_D();
		return fragment;
	}

}
